package com.ysh.demo01;

import com.ysh.utils.JDBCUtils;

import javax.sql.DataSource;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.logging.Logger;
/*
    自定义数据库连接池技术：
        1.定义一个类，实现DataSource接口
        2. 定义一个容器，用于保存多个Connection对象
        3. 定义静态代码块，通过JDBC工具类获取10个连接保存到容器中
        4. 重写getConnection方法，从容器中获取一个连接并返回
        5. 定义getSize()方法，用于获取容器大小并返回
 */
public class MyDataSource implements DataSource {
    // 1.准备容器，用于保存多个连接对象
    // 线程安全
    private static List<Connection> pool = Collections.synchronizedList(new ArrayList<>());
    // 2.定义代码块，通过工具类获取10个连接对象
    static{
        for (int i = 1; i <= 10; i++) {
            Connection conn = JDBCUtils.getConnection();
            pool.add(conn);
        }
    }
    // 3. 重写getConnection，用于获取连接对象
    @Override
    public Connection getConnection() throws SQLException {
        if (pool.size() > 0){
            Connection con = pool.remove(0);
            return con;
        }else{
            throw new RuntimeException("连接数量已达最大限制");
        }
    }
    // 4. 定义getSize方法，获取连接池容器的大小
    public int getSize(){
        return pool.size();
    }

    @Override
    public Connection getConnection(String username, String password) throws SQLException {
        return null;
    }

    @Override
    public <T> T unwrap(Class<T> iface) throws SQLException {
        return null;
    }

    @Override
    public boolean isWrapperFor(Class<?> iface) throws SQLException {
        return false;
    }

    @Override
    public PrintWriter getLogWriter() throws SQLException {
        return null;
    }

    @Override
    public void setLogWriter(PrintWriter out) throws SQLException {

    }

    @Override
    public void setLoginTimeout(int seconds) throws SQLException {

    }

    @Override
    public int getLoginTimeout() throws SQLException {
        return 0;
    }

    @Override
    public Logger getParentLogger() throws SQLFeatureNotSupportedException {
        return null;
    }
}
